<?php

error_reporting ( 7 );
set_magic_quotes_runtime ( 0 );
define ( 'SYSTEM_ROOTPATH', substr ( dirname ( __FILE__ ), 0, - 8 ) ); //定义根目录

$config = array ('DebugMode' => true, 'charset' => 'utf-8', 'timezone_default' => 'Asia/Shanghai' );
if (! $HeaderCharset) {
	header ( 'Content-Type: text/html; charset=' . $config ['charset'] );
}
$templates_cfg = array ('templates_dir' => '/templates', 'templates_compiled_dir' => '/templats_c/website', 'realrootpath' => SYSTEM_ROOTPATH, 'template_file_ext' => '.htm', 'template_compiled_file_ext' => '.tpl.php', 'templates_default_dir' => null );

if ($config ['DebugMode']) {
	ini_set ( 'display_errors', 1 );
} //启用日志

include SYSTEM_ROOTPATH . '/include/template.functions.php'; //模版编译类
include SYSTEM_ROOTPATH . '/include/class.nl_exception.php'; //错误日志
include SYSTEM_ROOTPATH . '/include/global.functions.php'; //常用函数
include SYSTEM_ROOTPATH . '/include/class.databasedriver_mysql.php'; //数据库类
include SYSTEM_ROOTPATH . '/include/database_config.php'; //数据库连接
include SYSTEM_ROOTPATH . '/include/h_cache_config.php';			//载入天气缓存配置文件
include SYSTEM_ROOTPATH . '/include/template.php'; //分页函数
include SYSTEM_ROOTPATH . '/include/get_weather.php'; //采集天气
include SYSTEM_ROOTPATH . '/include/tq.php'; //天气网函数
include SYSTEM_ROOTPATH . '/include/weather_cache.php'; //获取天气函数
include SYSTEM_ROOTPATH . '/include/get_weather_array.php'; //采集天气

//如果php版本大于5.1则使用北京时间（而不是系统默认的格林威治标准时间） 等同在php.ini的date.timezone语句前的;号去掉
if (PHP_VERSION > '5.1') {
	date_default_timezone_set ( $config ['timezone_default'] );
}
//get和post提交字符串处理
//if ($_GET)
//	$_GET = nl_addslashes ( $_GET );
//if ($_POST)
//	$_GET = nl_addslashes ( $_POST );

$_NL_DebugMode = $config ['DebugMode'];
//错误日志目录
//$__NL__LOG_SAVE_PATH = SYSTEM_ROOTPATH . '/log';


//域名后面的地址
$SCRIPT_URL = $_SERVER ['REQUEST_URI'] ? $_SERVER ['REQUEST_URI'] : $_SERVER ['SCRIPT_NAME'] . ($_SERVER ['QUERY_STRING'] ? "?" . $_SERVER ['QUERY_STRING'] : '');
//域名（主机名）
$SCRIPT_HOST = $_SERVER ["HTTP_HOST"];

//页面地址
$REQUEST_URI = 'http://' . $SCRIPT_HOST . ($_SERVER ["SERVER_PORT"] == 80 ? '' : $_SERVER ["SERVER_PORT"]) . (isset ( $_SERVER ['REQUEST_URI'] ) ? $_SERVER ['REQUEST_URI'] : $_SERVER ['PHP_SELF'] . '?' . $_SERVER ['QUERY_STRING']);

//if(strstr($SCRIPT_URL,"/weather.php?citypinyinm=")){
//$chenshi = str_replace ( "/weather.php?citypinyinm=", "", $SCRIPT_URL );

//header ( 'Location:http://www.tianqiyubao.org/weather_'.$chenshi );
//}
if(strstr($SCRIPT_URL,"/weather.php?citypinyinm=")){
$chenshi = str_replace ( "/weather.php?citypinyinm=", "", $SCRIPT_URL );

header ( 'Location:http://www.tianqiyubao.org/weatherm_'.$chenshi );
}elseif(strstr($SCRIPT_URL,"/weather.php?citypinyinf=")){
$chenshi = str_replace ( "/weather.php?citypinyinf=", "", $SCRIPT_URL );

header ( 'Location:http://www.tianqiyubao.org/weatherf_'.$chenshi );
}elseif(strstr($SCRIPT_URL,"/weather.php?citypinyint=")){
$chenshi = str_replace ( "/weather.php?citypinyint=", "", $SCRIPT_URL );

header ( 'Location:http://www.tianqiyubao.org/weathert_'.$chenshi );
}elseif(strstr($SCRIPT_URL,"/weather.php?citypinyins=")){
$chenshi = str_replace ( "/weather.php?citypinyins=", "", $SCRIPT_URL );

header ( 'Location:http://www.tianqiyubao.org/weathers_'.$chenshi );
}elseif(strstr($SCRIPT_URL,"/weather.php?citypinyin=")){
$chenshi = str_replace ( "/weather.php?citypinyin=", "", $SCRIPT_URL );

//header ( 'Location:http://www.tianqiyubao.org/weather_'.$chenshi );
}

//if($SCRIPT_HOST=='tianqiyubao.org'){
//header ( 'Location:http://www.tianqiyubao.org'.$SCRIPT_URL );
//}
//时间戳定义
$timestamp = time();
$timestamp = time();
$year=date("Y",$timestamp);
$month=date("m",$timestamp);
$day=date("d",$timestamp);
$list['adddate']=date("m-d",$list['adddate']);

//初始化数据库连接类
$db = new DatabaseDriver_MySql ( $database_cfg );
$now_table = getweathertable(date('y',time()));

//如果缓存方式为内存缓存，则初始化memcache
if($CACHE_TYPE == '1')
{

	$mem = new Memcache;
	$mem->connect('127.0.0.1');
}
//$_COOKIE ['wcity']['pinyin']='beijing';
//$_COOKIE ['wcity']['id']='110000';
//$_COOKIE ['wcity']['name']='北京';

//$weather = get_weather_date ( $_COOKIE ['wcity']['id'] );
$weekarray=array("日","一","二","三","四","五","六");
/**
 * 获取数据表,如果不存在则创建
 * @param weatherlog_ $id 后缀
 */
function getweathertable($id)
{
	global $db;
	global $table_top;

	if(!mysql_fetch_array($db->query("show tables like '{$table_top}weatherlog_{$id}'")))
	{
		$db->query("CREATE TABLE  `{$table_top}weatherlog_{$id}` (
		`id` INT( 100 ) NOT NULL AUTO_INCREMENT ,
		`AreaID` INT( 10 ) NOT NULL ,
		`Weather` TEXT NOT NULL ,
		`live` TEXT NOT NULL ,
		`xiangguan` TEXT NOT NULL ,
		`Update` DATE NOT NULL ,
		PRIMARY KEY (  `id` ) ,
		KEY  `AreaID` (  `AreaID` ) ,
		KEY  `Update` (  `Update` )
		) ENGINE = MYISAM DEFAULT CHARSET = utf8;
		");
}
return "{$table_top}weatherlog_{$id}";
}
/**
 * 返回某天的历史记录数据
 * @param 20120101 $date 数字格式的日期
 * @param  $pinyin 要返回的数据的拼音 
 */
function gethistorydata($date,$pinyin)
{
	global $db;
	global $tq_tu1;
	global $tq_tu2;
	$date = mktime(0,0,0,substr($date,4,2),substr($date,6,2),substr($date,0,4));		//日期
	$DB_table = getweathertable(date('y',$date));		//数据表
	$Area = Getone_Area_Pinyin ( $pinyin ); //地区详情
	$_Weather = $db->getone("select * from {$DB_table} where `AreaID` = '{$Area[id]}' and `Update` = '".date('Y-m-d',$date)."' and Weather not like '%null%' and Weather <> '' and live <> ''");
	$_Weather or $_Weather = $db->getone("select * from {$DB_table} where `AreaID` = '{$Area[parentid]}' and `Update` = '".date('Y-m-d',$date)."' and Weather not like '%null%' and Weather <> '' and live <> ''");
	$_Weather['Weather'] = str_replace ( "@", "\\", $_Weather['Weather'] );
	$_Weather['Weather'] = json_decode($_Weather['Weather'],true);
	$_Weather['live'] = json_decode($_Weather['live'],true);
	$Weather['Weather'] = array(
			0=>'/images/weather/'.$tq_tu1[$_Weather['Weather'][0]]['img'],	//白天的img图标
			1=> $tq_tu1[$_Weather['Weather'][0]]['name'],					//白天的天气名称
			2=>$_Weather['Weather'][1],										//白天温度
			3=>$_Weather['Weather'][2],										//白天风向
			4=>$_Weather['Weather'][3],										//白天风力
			5=>'/images/weather/'.$tq_tu2[$_Weather['Weather'][4]]['img'],	//晚上的天气img
			6=>$tq_tu1[$_Weather['Weather'][4]]['name'],					//晚上的天气名称
			7=>$_Weather['Weather'][5],										//晚上的温度
			8=>$_Weather['Weather'][6],										//晚上的风向
			9=>$_Weather['Weather'][7],										//晚上的风力
	);
	$Weather['live']=$_Weather['live'];
	$Weather['Update'] = $date;			
	return $Weather;
	
}
$area_cache_py = getarea_for_py();
$area_cache_id = getarea_for_id();				//area for id

function getarea_for_py()
{
	global $area_cache_base;
	global $db;
	global $table_top;
	$area_cache_py = $area_cache_base."py.txt";			//area for pinyin
	if(!file_exists($area_cache_py))
	{
		$data = $db->select('SELECT * from ' . $table_top . 'area');
		foreach($data as $v)
		{
			$arr[$v['pinyin']] = $v;
		}
		ob_start();
		var_export($arr);
		$f = '<?php  return $arr_py = '.ob_get_contents() . " ?>";
		ob_end_clean();
		
		file_put_contents($area_cache_py, $f);
		chmod($area_cache_py,0666);
	}
	require_once $area_cache_py;
	return $arr_py;
}
function getarea_for_id()
{
	global $area_cache_base;
	global $db;
	global $table_top;
	$area_cache_py = $area_cache_base."id.txt";
	if(!file_exists($area_cache_py))
	{
		$data = $db->select('SELECT * from ' . $table_top . 'area');
		foreach($data as $v)
		{
			$arr[$v['id']] = $v;
		}
		ob_start();
		var_export($arr);
		$f = '<?php  return $arr_id = '.ob_get_contents() . " ?>";
		ob_end_clean();
		
		file_put_contents($area_cache_py, $f);
		chmod($area_cache_py,0666);
		
		
	}
	require_once $area_cache_py;
	return $arr_id;
}
//include_once('getall.cron.php');		现在不需要这个函数了，注释掉
include_once('mailweather.cron.php');

?>